﻿/**
ugentool main entry point.

Authors: Kalman Kiss, kiskami@freemail.hu

Bugs:

Date: 

License: GNU General Public License

YASTACS game
Copyright (C) 2012 Kalman Kiss, kiskami@freemail.hu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Contact author at: kiskami@freemail.hu or
Hungary, 8900 Zalaegerszeg
Kakukkfu u 4.
*/
module main;

debug import std.stdio;
import std.datetime;
import std.getopt;

import ugen.UniversumGenerator;
import ugen.UniversumData;

immutable string DEFSEED = "55555";
immutable string DEFFILENAME = "ugentool-export-seed-55555.dat";
immutable string DEFJSONFILENAME = "ugentool-export-seed-55555.json";

string seed = DEFSEED;
string outputfilename = DEFFILENAME;
string jsonoutputfilename = DEFJSONFILENAME;

bool pretty = true;

class logger {
	void progress(float p) {
//		write(" (",p*100,") ");
	}
	void log(string msg) {
//		write("");
	}
}

UniversumGenerator ug;

void gen() {

	auto l = new logger();
	ug = new UniversumGenerator(seed);
	ug.setProgressDelegate(&l.progress);
	ug.setLogDelegate(&l.log);
	ug.gen();
}

void saveToFile(string filename, string str) {
	auto f = File(filename, "w"); // open for writing
	f.writeln(str);
}

void savethem() {
	saveToFile(outputfilename, ug.universum.exportAsGNUPlot());
	saveToFile(jsonoutputfilename, ug.universum.toJson(pretty));
}

string json;
void genJson() {
		json = ug.universum.toJson(pretty);
}

Universum newu;
void genFromJson() {
	newu = Universum.createFromJson(json);
}

int main(string[] argv)
{
	debug writeln("ugentool start");
	debug scope(exit) writeln ("ugentool end");

	bool help = false;

	getopt(argv, "seed|s", &seed, "outfile|o", &outputfilename, "jsonoutfile|j", &jsonoutputfilename, "help|?", &help);

	if(help) {
		writeln("Usage: [-s|seed] [-o|outfilename] [-j|jsonoutfile] [-?|help]");
		return 1;
	}

	debug {
		int n = 1;
		auto r = benchmark!(gen)(n);
		writefln("Seconds to call ug.gen %s times: %ssec", n, r[0].to!("seconds", float));

		// test json serialisation
		auto r2 = benchmark!(genJson)(n);
		writefln("Seconds to call genJson %s times: %ssec", n, r2[0].to!("seconds", float));

		writeln("Generated json.length=", json.length);
		saveToFile("tesz1.json",json);

		auto r3 = benchmark!(genFromJson)(n);
		writefln("Seconds to call genFromJson %s times: %ssec", n, r3[0].to!("seconds", float));

		auto json2 = newu.toJson(pretty);
		writeln("Generated json2.length=", json2.length);
		saveToFile("tesz2.json",json2);

	} else
		gen();

	savethem();

	return 0;
}
